package com.ledi.guns.modular.ledipay.controller;

import io.swagger.annotations.Api;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.plugins.Page;
import com.ledi.guns.common.annotion.Permission;
import com.ledi.guns.common.persistence.model.vo.AgentCountDataVo;
import com.ledi.guns.common.persistence.model.vo.AgentInfoVo;
import com.ledi.guns.common.persistence.model.vo.AgentNamesVo;
import com.ledi.guns.common.persistence.model.vo.AgentUserVo;
import com.ledi.guns.common.persistence.model.vo.HotAgentVo;
import com.ledi.guns.core.base.controller.BaseController;
import com.ledi.guns.core.base.tips.SuccessTip;
import com.ledi.guns.core.base.tips.Tip;
import com.ledi.guns.modular.ledipay.controller.req.H5ApplySchool.AgentResult;
import com.ledi.guns.modular.ledipay.controller.req.agent.AddAgentInfo;
import com.ledi.guns.modular.ledipay.controller.req.agent.AddAgentUser;
import com.ledi.guns.modular.ledipay.controller.req.agent.EditAgentInfo;
import com.ledi.guns.modular.ledipay.controller.req.agent.QueryAgentCountData;
import com.ledi.guns.modular.ledipay.controller.req.agent.QueryAgentId;
import com.ledi.guns.modular.ledipay.controller.req.agent.QueryAgentInfo;
import com.ledi.guns.modular.ledipay.controller.req.agent.QueryAgentNames;
import com.ledi.guns.modular.ledipay.controller.req.agent.QueryHotData;
import com.ledi.guns.modular.ledipay.controller.req.agent.QuerySeeAgentInfo;
import com.ledi.guns.modular.ledipay.service.H5ApplySchoolService;
import com.ledi.guns.modular.ledipay.service.impl.AgentServiceImpl;

/**
 * 代理商后台控制器
 *
 * @author sunwu
 * @Date 2017-11-29 10:53:00
 */
@Api
@RestController
@RequestMapping("/agent")
public class AgentController extends BaseController {

	@Autowired
	private AgentServiceImpl agentServiceImpl;

	@Autowired
	private H5ApplySchoolService h5ApplySchoolService;

	/**
	 * @api {POST} /agent/list 代理商列表
	 * @apiGroup agent
	 * @apiName list
	 * @apiParam {String} agentName 代理商名称
	 * @apiParam {String} contactPeople 联系人
	 * @apiParam {String} contactTel 联系方式
	 * @apiParam {String} province 省
	 * @apiParam {String} city 市
	 * @apiParam {String} area 区
	 * @apiSuccess {Long} agentId 代理商ID
	 * @apiSuccess {String} province 省
	 * @apiSuccess {String} city 市
	 * @apiSuccess {String} area 区
	 * @apiSuccess {String} contactPeople 联系人
	 * @apiSuccess {String} contactTel 联系方式
	 * @apiSuccess {String} remarks 备注
	 * @apiSuccess {String} remarks 备注
	 * @apiSuccess {Integer} versionNo 版本号
	 * @apiSuccess {Integer} flow 总流水
	 * @apiSuccess {Integer} aliFlow 支付宝流水
	 * @apiVersion 1.0.0
	 * @apiParamExample {json} Request-Example:
	 *                  {
	 *                  "agentId": "XX",
	 *                  "contactPeople":"张三"
	 *                  "contactTel":"13888888888"
	 *                  "province":"江苏省"
	 *                  "city":"苏州市"
	 *                  "area":"园区"
	 *                  }
	 * @apiSuccessExample {json} Success-Response:
	 *                    {
	 *                    "agentId": "1",
	 *                    "area": "吴中区",
	 *                    "city": "苏州市",
	 *                    "contactPeople": "张三",
	 *                    "contactTel": "18813841384",
	 *                    "province": "江苏省",
	 *                    "remarks": "很强的政府关系",
	 *                    "schoolCount": 0
	 *                    }
	 */
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	@Permission
	public Tip list(@RequestBody QueryAgentInfo queryAgentInfo) {

		queryAgentInfo.checkOrder(AgentInfoVo.class);
		Page<AgentInfoVo> page = agentServiceImpl.getAgentInfoList(new Page<AgentInfoVo>(queryAgentInfo.getPageNo(), queryAgentInfo.getPageSize()), queryAgentInfo);
		return SuccessTip.buildSuccess(page);
	}

	/**
	 * @api {POST} /agent/add 代理商增加
	 * @apiGroup agent
	 * @apiName add
	 * @apiParam {String} accountsBank 开户行
	 * @apiParam {Integer} agentType 代理商类型(1:代理商 2:园所合伙人)
	 * @apiParam {Integer} schoolCount 学校数量
	 * @apiParam {Integer} schoolCountLimit 园所数量限制
	 * @apiParam {String} agentName 代理商名称
	 * @apiParam {String} cardNum 银行卡号
	 * @apiParam {String} province 省
	 * @apiParam {String} city 市
	 * @apiParam {String} area 区
	 * @apiParam {String} contactPeople 联系人
	 * @apiParam {String} contactTel 联系方式(手机号码)
	 * @apiParam {String} loginPasswd 登陆密码
	 * @apiParam {String} openProvince 开户省
	 * @apiParam {String} openCity 开户市
	 * @apiParam {String} remarks 备注
	 * @apiParam {String} specificAddr 具体地址
	 * @apiVersion 1.0.0
	 * @apiParamExample {json} Request-Example:
	 *                  {
	 *                  "accountsBank": "string",
	 *                  "agentName": "string",
	 *                  "area": "string",
	 *                  "cardNum": "string",
	 *                  "city": "string",
	 *                  "contactPeople": "string",
	 *                  "contactTel": "string",
	 *                  "loginPasswd": "string",
	 *                  "openArea": "string",
	 *                  "province": "string",
	 *                  "remarks": "string",
	 *                  "specificAddr": "string"
	 *                  }
	 * @apiSuccessExample {json} Success-Response:
	 *                    {
	 *                    "code": 200,
	 *                    "message": "成功"
	 *                    }
	 */
	@Permission
	@RequestMapping(value = "add", method = RequestMethod.POST)
	public Tip add(@RequestBody AddAgentInfo addAgentInfo) {

		agentServiceImpl.addAgentInfo(addAgentInfo);
		return SuccessTip.buildSuccess();
	}

	/**
	 * @api {POST} /agent/getAgentNames 获取代理商名称
	 * @apiGroup agent
	 * @apiName getAgentNames
	 * @apiParam {String} name 名称
	 * @apiVersion 1.0.0
	 * @apiParamExample {json} Request-Example:
	 *                  {
	 *                  "name": "大光"
	 *                  }
	 * @apiSuccessExample {json} Success-Response:
	 *                    {
	 *                    "code": 200,
	 *                    "data": [
	 *                    {
	 *                    "agentName": "大光代理商",
	 *                    "area": "4641",
	 *                    "city": "41238",
	 *                    "province": "1354"
	 *                    }
	 *                    ],
	 *                    "message": "成功"
	 *                    }
	 */
	@Permission
	@RequestMapping(value = "/getAgentNames", method = RequestMethod.POST)
	public Tip getAgentNames(@RequestBody QueryAgentNames queryAgentNames) {

		List<AgentNamesVo> agentNamesVos = agentServiceImpl.getAgentNames(queryAgentNames);
		return SuccessTip.buildSuccess(agentNamesVos);
	}

	/**
	 * @api {POST} /agent/getAgentId 获取代理商ID
	 * @apiGroup agent
	 * @apiName getAgentId
	 * @apiParam {String} name 名称
	 * @apiVersion 1.0.0
	 * @apiParamExample {json} Request-Example:
	 *                  {
	 *                  "name": "大光"
	 *                  }
	 * @apiSuccessExample {json} Success-Response:
	 *                    {
	 *                    "code": 200,
	 *                    data": 9,
	 *                    "message": "成功"
	 *                    }
	 */
	@Permission
	@RequestMapping(value = "/getAgentId", method = RequestMethod.POST)
	public Tip getAgentId(@RequestBody QueryAgentId queryAgentId) {

		Long agentId = agentServiceImpl.getAgentId(queryAgentId);
		return SuccessTip.buildSuccess(agentId);
	}

	/**
	 * @api {GET} /agent/getAgentStatisticalData 获取统计数据
	 * @apiGroup agent
	 * @apiName getAgentStatisticalData
	 * @apiSuccess {int } agentCount 代理商总数
	 * @apiSuccess {int } flow 代理商总流水
	 * @apiSuccess {int } aliFlow 支付宝流水
	 * @apiSuccess {int } schoolCount 开通学校数目
	 * @apiVersion 1.3.0
	 * @apiSuccessExample {json} Success-Response:
	 *                    {
	 *                    "code": 200,
	 *                    "data": {
	 *                    "agentCount": 4,
	 *                    "flow": 0,
	 *                    "schoolCount": 1
	 *                    },
	 *                    "message": "成功"
	 *                    }
	 */
	@Permission
	@RequestMapping(value = "/getAgentStatisticalData", method = RequestMethod.GET)
	public Tip getAgentStatisticalData() {

		return SuccessTip.buildSuccess(agentServiceImpl.getStatisticalData(null, null, 0));
	}

	/**
	 * @api {POST} /agent/getAgentStatisticalData 获取统计代理学校流水数据
	 * @apiGroup agent
	 * @apiName getAgentStatisticalData
	 * @apiParam {Date} startTime 开始时间
	 * @apiParam {Date} startTime 结束时间
	 * @apiName postAgentStatisticalData
	 * @apiSuccess {int } agentCount 代理商总数
	 * @apiSuccess {int } flow 代理商总流水
	 * @apiSuccess {int } aliFlow 支付宝流水
	 * @apiSuccess {int } weixinFlow 微信流水
	 * @apiSuccess {int } undergroundFlow 线下流水
	 * @apiVersion 1.3.0
	 * @apiSuccessExample {json} Success-Response:
	 *                    {
	 *                    "code": 200,
	 *                    "data": {
	 *                    "agentCount": 4,
	 *                    "flow": 0,
	 *                    "schoolCount": 1
	 *                    },
	 *                    "message": "成功"
	 *                    }
	 */
	@Permission
	@RequestMapping(value = "/getAgentStatisticalData", method = RequestMethod.POST)
	public Tip getAgentStatisticalData(@RequestBody QueryHotData queryHotData) {

		return SuccessTip.buildSuccess(agentServiceImpl.getStatisticalData(queryHotData.getStartTime(), queryHotData.getEndTime(), 1));
	}

	/**
	 * @api {POST} /agent/getAllAgentStatisticalData 获取统计所有代理学校流水数据
	 * @apiGroup agent
	 * @apiName getAllAgentStatisticalData
	 * @apiParam {Date} startTime 开始时间
	 * @apiParam {Date} startTime 结束时间
	 * @apiName postAgentStatisticalData
	 * @apiSuccess {int } agentCount 代理商总数
	 * @apiSuccess {int } flow 代理商总流水
	 * @apiSuccess {int } aliFlow 支付宝流水
	 * @apiSuccess {int } weixinFlow 微信流水
	 * @apiSuccess {int } undergroundFlow 线下流水
	 * @apiVersion 1.3.0
	 * @apiSuccessExample {json} Success-Response:
	 *                    {
	 *                    "code": 200,
	 *                    "data": {
	 *                    "agentCount": 4,
	 *                    "flow": 0,
	 *                    "schoolCount": 1
	 *                    },
	 *                    "message": "成功"
	 *                    }
	 */
	@Permission
	@RequestMapping(value = "/getAllAgentStatisticalData", method = RequestMethod.POST)
	public Tip getAllAgentStatisticalData(@RequestBody QueryHotData queryHotData) {

		return SuccessTip.buildSuccess(agentServiceImpl.getStatisticalData(queryHotData.getStartTime(), queryHotData.getEndTime(), 2));
	}

	/**
	 * @api {POST} /agent/getHotAgentData 获取热门代理商数据(TOP10)
	 * @apiGroup agent
	 * @apiName getHotAgentData
	 * @apiParam {Date} startTime 开始时间
	 * @apiParam {Date} endTime 结束时间
	 * @apiVersion 1.0.0
	 * @apiParamExample {json} Request-Example:
	 *                  {
	 *                  "startTime": "2017-11-11 14:42:18",
	 *                  "endTime":"2017-12-11 14:42:18"
	 *                  }
	 * @apiSuccessExample {json} Success-Response:
	 *                    {
	 *                    "code": 200,
	 *                    "data": [
	 *                    {
	 *                    "agentName": "苏州地区代理商",
	 *                    "chargeAmount": 100,
	 *                    "cityName": "",
	 *                    "districtName": "",
	 *                    "provinceName": "",
	 *                    "tollCollection": 3,
	 *                    "underSchoolCount": 1
	 *                    },
	 *                    {
	 *                    "agentName": "静冈代理商",
	 *                    "chargeAmount": 97.03,
	 *                    "cityName": "",
	 *                    "districtName": "",
	 *                    "provinceName": "",
	 *                    "tollCollection": 2,
	 *                    "underSchoolCount": 1
	 *                    }
	 *                    ],
	 *                    "message": "成功"
	 *                    }
	 */
	@Permission
	@RequestMapping(value = "/getHotAgentData", method = RequestMethod.POST)
	public Tip getHotAgentData(@RequestBody QueryHotData queryHotData) {

		Date startTime = queryHotData.getStartTime();
		Date endTime = queryHotData.getEndTime();
		List<HotAgentVo> hotAgentVos = agentServiceImpl.getHotAgentData(startTime, endTime);
		return SuccessTip.buildSuccess(hotAgentVos);
	}

	/**
	 * @api {POST} /agent/getAgentTotalData 获取代理商首页统计数据
	 * @apiGroup agent
	 * @apiName getAgentTotalData
	 * @apiParam {Long} userId 当前代理商用户的ID
	 * @apiVersion 1.0.0
	 * @apiSuccessExample {json} Success-Response:
	 *                    {
	 *                    "code": 200,
	 *                    "data": {
	 *                    "agentTotalFlow": 132.03,
	 *                    "schoolTotalCount": 8
	 *                    },
	 *                    "message": "成功"
	 *                    }
	 */
	@Permission
	@RequestMapping(value = "/getAgentTotalData", method = RequestMethod.POST)
	public Tip getAgentTotalData(@RequestBody QueryAgentCountData queryAgentCountData) {

		AgentCountDataVo agentCountDataVo = agentServiceImpl.getAgentCountData(queryAgentCountData);
		return SuccessTip.buildSuccess(agentCountDataVo);
	}

	/**
	 * @api {POST} /agent/editAgentInfo 代理商编辑
	 * @apiGroup agent
	 * @apiName editAgentInfo
	 * @apiParam {String} accountsBank 开户行
	 * @apiParam {Integer} schoolCountLimit 园所数量限制
	 * @apiParam {String} agentName 代理商名称
	 * @apiParam {String} cardNum 银行卡号
	 * @apiParam {String} province 省
	 * @apiParam {String} city 市
	 * @apiParam {String} area 区
	 * @apiParam {String} contactPeople 联系人
	 * @apiParam {String} contactTel 联系方式(手机号码)
	 * @apiParam {String} loginPasswd 登陆密码
	 * @apiParam {String} openProvince 开户省
	 * @apiParam {String} openCity 开户市
	 * @apiParam {String} remarks 备注
	 * @apiParam {String} specificAddr 具体地址
	 * @apiVersion 1.1.0
	 * @apiParamExample {json} Request-Example:
	 *                  * {
	 *                  "agentId": "XX",
	 *                  "contactPeople":"张三"
	 *                  "contactTel":"13888888888"
	 *                  "province":"江苏省"
	 *                  "city":"苏州市"
	 *                  "area":"园区"
	 *                  }
	 * @apiSuccessExample {json} Success-Response:
	 *                    {
	 *                    "code": 200,
	 *                    "message": "成功"
	 *                    }
	 */
	@Permission
	@RequestMapping(value = "editAgentInfo", method = RequestMethod.POST)
	public Tip editAgentInfo(@RequestBody EditAgentInfo editAgentInfo) {

		agentServiceImpl.editAgentInfo(editAgentInfo);
		return SuccessTip.buildSuccess();
	}

	/**
	 * @api {DELETE} /agent/deleteAgentInfo/{id} 代理商删除
	 * @apiGroup agent
	 * @apiName deleteAgentInfo
	 * @apiParam {long} id 主键ID
	 * @apiVersion 1.1.0
	 * @apiParamExample {json} Request-Example:
	 *                  * {
	 *                  "": "XX"
	 *                  }
	 * @apiSuccessExample {json} Success-Response:
	 *                    {
	 *                    "code": 200,
	 *                    "message": "成功"
	 *                    }
	 */
	@Permission
	@RequestMapping(value = "deleteAgentInfo/{id}", method = RequestMethod.DELETE)
	public Tip deleteAgentInfo(@PathVariable("id") Integer id) {

		agentServiceImpl.deleteAgentInfo(id);
		return SuccessTip.buildSuccess();
	}

	/**
	 * @api {POST} /agent/addAgentUser 增加代理商人员
	 * @apiGroup agent
	 * @apiName addAgentUser
	 * @apiParam {String} contactName 联系人名称
	 * @apiParam {String} contactTel 联系人电话号码
	 * @apiParam {String} loginPasswd 登陆密码
	 * @apiParam {String} remark 备注
	 * @apiVersion 1.1.0
	 * @apiParamExample {json} Request-Example:
	 *                  * {
	 *                  "contactName": "XX",
	 *                  "contactTel":"13888888888"
	 *                  "loginPasswd":"111111"
	 *                  "remark":"江苏省"
	 *                  }
	 * @apiSuccessExample {json} Success-Response:
	 *                    {
	 *                    "code": 200,
	 *                    "message": "成功"
	 *                    }
	 */
	@Permission
	@RequestMapping(value = "addAgentUser", method = RequestMethod.POST)
	public Tip addAgentUser(@RequestBody AddAgentUser addAgentUser) {

		agentServiceImpl.addAgentUser(addAgentUser);
		return SuccessTip.buildSuccess();
	}

	/**
	 * @api {POST} /agent/seeAgentInfo 查看代理商信息
	 * @apiGroup agent
	 * @apiName seeAgentInfo
	 * @apiParam {long} agentId 代理商主键ID
	 * @apiVersion 1.1.0
	 * @apiParamExample {json} Request-Example:
	 *                  * {
	 *                  "agentId": 16
	 *                  }
	 * @apiSuccessExample {json} Success-Response:
	 *                    {
	 *                    "code": 200,
	 *                    "data": {
	 *                    "accountsBank": "test",
	 *                    "agentName": "test",
	 *                    "area": "吴中区",
	 *                    "cardNum": "6220546448454",
	 *                    "city": "苏州市",
	 *                    "contactPeople": "张老汉",
	 *                    "contactTel": "18554617945",
	 *                    "openCity": "市辖区",
	 *                    "openProvince": "北京市",
	 *                    "province": "江苏省",
	 *                    "remarks": "我很强",
	 *                    "specificAddr": "test",
	 *                    "versionNo": 0
	 *                    },
	 *                    "message": "成功"
	 *                    }
	 */
	@Permission
	@RequestMapping(value = "/seeAgentInfo", method = RequestMethod.POST)
	public Tip seeAgentInfo(@RequestBody QuerySeeAgentInfo querySeeAgentInfo) {

		return SuccessTip.buildSuccess(agentServiceImpl.seeAgentInfo(querySeeAgentInfo));
	}

	/**
	 * @api {POST} /agent/getAgentUserList 查看代理商员工列表
	 * @apiGroup agent
	 * @apiName getAgentUserList
	 * @apiParam {string} orderBy 排序
	 * @apiParam {string} orderType 排序类型
	 * @apiParam {int} pageNo 分页数
	 * @apiParam {int} pageSize 分页大小
	 * @apiVersion 1.1.0
	 * @apiParamExample {json} Request-Example:
	 *                  {
	 *                  "orderBy": "string",
	 *                  "orderType": "string",
	 *                  "pageNo": 0,
	 *                  "pageSize": 0
	 *                  }
	 * @apiSuccessExample {json} Success-Response:
	 *                    {
	 *                    "code": 200,
	 *                    "data": {
	 *                    "count": 5,
	 *                    "firstResult": 0,
	 *                    "list": [
	 *                    {
	 *                    {
	 *                    "createName": "张三",
	 *                    "createTime": "2018-01-08 16:25:28",
	 *                    "id": 135,
	 *                    "isDeleteStatus": 0,
	 *                    "remark": "",
	 *                    "roleName": "代理商员工",
	 *                    "userName": "小宝宝",
	 *                    "userTel": "15548651492"
	 *                    },
	 *                    {
	 *                    "createName": "",
	 *                    "createTime": "2016-01-29 08:49:53",
	 *                    "id": 1,
	 *                    "isDeleteStatus": 0,
	 *                    "remark": "",
	 *                    "roleName": "1,6",
	 *                    "userName": "张三",
	 *                    "userTel": "admin"
	 *                    }
	 *                    ],
	 *                    "orderBy": "id",
	 *                    "orderType": "DESC",
	 *                    "pageNo": 1,
	 *                    "pageSize": 10,
	 *                    "search": {
	 *                    "orderType": "DESC",
	 *                    "agentId": 1,
	 *                    "defaultPageSize": 10,
	 *                    "pageNo": 1,
	 *                    "pageSize": 10,
	 *                    "orderBy": "id",
	 *                    "firstResult": 0
	 *                    },
	 *                    "total": 5
	 *                    },
	 *                    "message": "成功"
	 *                    }
	 */
	@Permission
	@RequestMapping(value = "/getAgentUserList", method = RequestMethod.POST)
	public Tip getAgentUserList(@RequestBody QueryAgentInfo pageQuery) {

		pageQuery.checkOrder(AgentUserVo.class);
		Page<AgentUserVo> page = agentServiceImpl.agentUserList(new Page<AgentUserVo>(pageQuery.getPageNo(), pageQuery.getPageSize()), pageQuery);
		return SuccessTip.buildSuccess(page);
	}

	/**
	 * @api {DELETE} /agent/deleteAgentUser/{id} 代理商用户删除
	 * @apiGroup agent
	 * @apiName deleteAgentUser
	 * @apiParam {long} id 主键ID
	 * @apiVersion 1.1.0
	 * @apiParamExample {json} Request-Example:
	 *                  * {
	 *                  "": "XX"
	 *                  }
	 * @apiSuccessExample {json} Success-Response:
	 *                    {
	 *                    "code": 200,
	 *                    "message": "成功"
	 *                    }
	 */
	@Permission
	@RequestMapping(value = "/deleteAgentUser/{id}", method = RequestMethod.DELETE)
	public Tip deleteAgentUser(@PathVariable("id") Integer id) {

		agentServiceImpl.deleteAgentUser(id);
		return SuccessTip.buildSuccess();
	}

	/**
	 * @api {GET} /agent/queryAgent 跳转到代理商添加学校页面
	 * @apiGroup agent
	 * @apiName queryAgent
	 * @apiSuccess agentId 代理商Id
	 * @apiSuccess agentName 代理商名称
	 * @apiSuccess sign 签名
	 * @apiSuccessExample {json} error-Response:
	 *                    {
	 *                    "code": 200,
	 *                    "data": {
	 *                    "sign": "aasasds55adad5ad1ad",
	 *                    "agentId": "45",
	 *                    "agentName": "aaaa"
	 *                    },
	 *                    "message": "成功"
	 *                    }
	 * @apiVersion 1.1.0
	 */
	@Permission
	@RequestMapping(value = "/queryAgent", method = RequestMethod.GET)
	public Tip excelTemplate() {

		AgentResult AgentResult = h5ApplySchoolService.queryAgentId();

		return SuccessTip.buildSuccess(AgentResult);
	}
}
